package com.project_task.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.project_task.constant.TaskStatusConstant;
import com.project_task.dto.TaskDTO;
import com.project_task.dto.TaskPageQueryDTO;
import com.project_task.entity.Project;
import com.project_task.entity.Task;
import com.project_task.mapper.TaskMapper;
import com.project_task.result.PageResult;
import com.project_task.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Override
    public void save(TaskDTO taskDTO) {
        Task task = new Task();
        BeanUtils.copyProperties(taskDTO, task);
        // 任务状态设置为待办
        task.setStatus(TaskStatusConstant.STATUS_TODO);
        // 在任务表中添加相应的任务
        taskMapper.save(task);
    }

    @Override
    public PageResult pageQuery(TaskPageQueryDTO taskPageQueryDTO) {
        PageHelper.startPage(taskPageQueryDTO.getPage(), taskPageQueryDTO.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        Page<Project> page = taskMapper.pageQuery(taskPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }
}
